<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>公司用户管理</title>
    <jsp:include page="../inc/css.jsp"/>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <div class="content-wrapper">
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <div class="card-header">
                            <h4>${company.name}</h4>

                            <div class="form-inline" style="margin-top: 10px;">
                                <c:if test="${sessionScope.session_master_id == 1}">
                                    <div class="form-group">
                                        <label style="cursor: pointer;"><input type="radio" name="selectCompany" checked value="0">未分配</label>
                                        <label style="cursor: pointer;" class="tableTopBtn"><input type="radio" name="selectCompany" value="1">已分配</label>
                                    </div>
                                </c:if>

                                <div class="form-group tableTopBtn">
                                    <input id="searchKeyWord" placeholder="关键词查询" class="form-control"/>
                                </div>

                                <button type="button" title="查询" class="btn btn-info tableTopBtn" onclick="_childList.fn.reloadData()"> 查询</button>
                                <button type="button" title="重置搜索条件并查询" class="btn btn-success tableTopBtn" onclick="_childList.fn.resetAndReloadData()"> 重置</button>
                                <button type="button" title="清除所有联系人" class="btn btn-danger tableTopBtn" onclick="_childList.fn.cleanAllContact()">清除所有联系人</button>

                                <button type="button" title="返回公司列表" class="btn btn-warning tableTopBtn" onclick="_childList.fn.goBack()">返回</button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>昵称</th>
                                        <th>姓名</th>
                                        <th style="width: 50px;">性别</th>
                                        <th style="width: 100px;">手机号码</th>
                                        <th style="max-width: 50px">头像</th>
                                        <th>备注</th>
                                        <th style="max-width: 160px">注册时间</th>
                                        <th style="width: 80px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<input id="hiddenCompanyId" type="hidden" value="${company.id}"/>

<!-- page script -->
<script type="text/javascript">
    let _childList = {
        v: {
            list: [],
            dTable: null,
            currentMasterId: 0
        },
        fn: {
            init: function () {
                _childList.v.currentMasterId = Number($('#loginMasterId').val());

                _childList.fn.dataTableInit();
            },
            goBack: function () {
                window.history.go(-1);
            },
            reloadData: function () {
                _childList.v.list = [];
                _childList.v.dTable.DataTable().ajax.reload(null, false);
            },
            resetAndReloadData: function () {
                $('#searchKeyWord').val('');

                _childList.fn.reloadData();
            },
            cleanAllContact: function () {
                swal({
                    title: "您确定要清除该公司的所有联系人吗",
                    text: "删除后需重新逐个添加，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，清除！",
                    cancelButtonText: "不，取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        startLoading();

                        $.post('backend/company/cleanContact', {
                            companyId: $('#hiddenCompanyId').val()
                        }, function (result) {
                            endLoading();

                            if (result.code === 0) {
                                swal("清除成功！", "您已经清除了该公司的所有联系人信息。", "success");
                            } else {
                                info(result.msg, "error");
                            }
                        });
                    } else {
                        swal("已取消", "您取消了清除操作！", "error");
                    }
                })
            },
            dataTableInit: function () {
                _childList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许公司改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/company/child/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {
                            "data": "nickName",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {
                            "data": "name",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {
                            "data": "sex",
                            render: function (data) {
                                if (data === 1) {
                                    return '男';
                                } else {
                                    return '女';
                                }
                            }
                        },
                        {
                            "data": "mobile",
                            render: function (data) {
                                return formatStr(data, 11);
                            }
                        },
                        {
                            "data": "avatar",
                            render: function (data) {
                                return formatPicUrl(data);
                            }
                        },
                        {
                            "data": "description",
                            render: function (data) {
                                return formatStr(data, 20);
                            }
                        },
                        {
                            "data": "createTime",
                            render: function (data) {
                                return crtTimeFtt(data);
                            }
                        },
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='添加到公司' class='btn btn-sm edit'><i class='fa fa-recycle'></i></button>" +
                                "&nbsp;&nbsp;" +
                                "<button type='button' title='设为联系人' class='btn btn-success btn-sm change'><i class='fa fa-check'></i></button>",
                            "targets": -1
                        }
                    ],
                    "createdRow": function (row, data, index) {
                        _childList.v.list.push(data);
                    },
                    rowCallback: function (row, data) {
                        if (_childList.v.currentMasterId === 1 && $('input:radio:checked').val() === '0') {
                            $('td', row).last().find(".edit").prop('title', '添加到公司中');
                            $('td', row).last().find(".edit").addClass('btn-primary');
                        } else {
                            $('td', row).last().find(".edit").prop('title', '从公司中移除');
                            $('td', row).last().find(".edit").addClass('btn-danger');
                        }

                        $('td', row).last().find(".edit").click(function () {
                            _childList.fn.changeRelation(data.id);
                        });

                        $('td', row).last().find(".change").click(function () {
                            _childList.fn.setContact(data.id);
                        });
                    },
                    "fnServerParams": function (aoData) {
                        _childList.v.list = [];

                        if (_childList.v.currentMasterId === 1) {
                            aoData.type = $('input:radio:checked').val();
                        } else {
                            aoData.type = 1;
                        }
                        aoData.companyId = $('#hiddenCompanyId').val();
                        aoData.keyWord = $('#searchKeyWord').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            changeRelation: function (id) {
                let selectCompanyId;

                if (_childList.v.currentMasterId === 1) {
                    selectCompanyId = $('input:radio:checked').val();
                } else {
                    selectCompanyId = 0;
                }

                startLoading();

                $.post('backend/company/updateCompanyUser', {
                    ids: id,
                    selectCompanyId: selectCompanyId,
                    companyId: $('#hiddenCompanyId').val()
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("用户所属关系已变更", "success");

                        _childList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            setContact: function (id) {
                startLoading();

                $.post('backend/company/setContact', {
                    userId: id,
                    companyId: $('#hiddenCompanyId').val(),
                    type: 0
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("操作成功", "success");

                        _childList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            }
        }
    };

    $(document).ready(function () {
        _childList.fn.init();

        window.onload = function () {
            checkSelectMenu("companyLi");

            loadMenuColor('companyLi');
        }
    });
</script>
</body>
</html>